<?php
/**
 * date: 2023/1/25 22:32
 * user: chenlong<vip_chenlong@163.com>
 */

namespace App\Common\Util;

use App\Common\Util\Response\Json;
use Psr\Http\Message\ResponseInterface;
use think\facade\Route;
use think\response\File;
use think\response\Html;
use think\response\Redirect;
use think\response\View;

/**
 * @mixin Route
 */
class Response
{
    /**
     * @param array|null $data
     *
     * @return Json|ResponseInterface
     * @author chenlong<vip_chenlong@163.com>
     * @date   2023/1/26 01:03
     */
    public static function json(array $data = null): Json|\think\Response
    {
        $json = new Json();
        return $data === null ? $json : $json->raw($data);
    }

    /**
     * @param string|\Stringable $string
     *
     * @return \think\Response
     * @author chenlong<vip_chenlong@163.com>
     * @date   2023/1/26 01:03
     */
    public static function html(string|\Stringable $string): \think\Response
    {
        return \think\Response::create($string, Html::class);
    }

    /**
     * 重定向
     *
     * @param string $url
     * @param int    $code
     *
     * @return \think\Response|Redirect
     */
    public static function redirect(string $url = '', int $code = 302): \think\Response
    {
        return redirect($url, $code);
    }

    /**
     * 文件下载
     *
     * @param string $filename
     * @param string $name
     * @param bool   $content
     * @param int    $expire
     *
     * @return \think\Response|File
     */
    public static function download(string $filename, string $name = '', bool $content = false, int $expire = 180): \think\Response
    {
        return download(...func_get_args());
    }

    /**
     * 视图
     *
     * @param string $template
     * @param array  $vars
     * @param int    $code
     * @param        $filter
     *
     * @return \think\Response|View
     */
    public static function view(string $template = '', array $vars = [], int $code = 200, $filter = null): \think\Response
    {
        return view(...func_get_args());
    }

    /**
     * @param string $template
     * @param array  $vars
     *
     * @return string
     */
    public static function render(string $template, array $vars = []): string
    {
        return (new Response\View($template, $vars))->render();
    }
}
